<script type="text/javascript">
	function switchView(d){
		$("tbody").each(function(index){
			if(this.id.indexOf("edit_") > -1){
				$(this).hide();	
			}
			else{
				$(this).show();
			}
		});
		var i = d.id.substring(d.id.indexOf("_") + 1);
		
		if(d.id.indexOf("view_") > -1){
			$("#" + d.id).hide();
			$("#edit_" + i).show();	
		}
		else{
			$("#" + d.id).hide();
			$("#view_" + i).show();
		}
	}
	
	function textChange(d){
		var i = d.id.substring(d.id.indexOf("_") + 1);
		$("#span_" + i).html($(d).val());
	}
	
	function saveRow(e){
		var tbody = $(e).parent().parent().parent();
		
		var _orig = {};
		var _new = {};
		
		tbody.find("input[type=hidden]").each(function(index){
			var id = this.id;
			setVals(_orig,this.id);
		});
		tbody.find("input[type=text]").each(function(index){
			var id = this.id;
			setVals(_new,this.id);
		});
		data2Send = {"origFirst":_orig.firstName,
					"origLast":_orig.lastName,
					"origEmailAddr":_orig.emailAddress,
					"newFirst":_new.firstName,
					"newLast":_new.lastName,
					"newEmailAddr":_new.emailAddress};
		
		$.post("soap/UpdateDenormPlayer.php", data2Send ,function(data,textStatus){
			debugger;
			alert("Saved successfully!\n" + textStatus);
		}).error(function(jqXHR, textStatus, errorThrown){
			alert("Error\n" + textStatus + "\n" + errorThrown);
		});	
	}
	function setVals(o,id){
		var val = $("#" + id).val();
		if(id.indexOf("_first_") > -1){
			o.firstName = val;
		}
		else if(id.indexOf("_last_") > -1){
			o.lastName = val;
		}
		else if(id.indexOf("_emailAddr_") > -1){
			o.emailAddress = val;
		}
	}
	function insertNewPlayers(){
		alert("Hit!");
		$("#indexForm").submit();
	}
</script>
<p>Followings are new players found in the uploaded data. Click on the cell to edit the data.</p>
<div id="toolbar">

	<?php printf("<input type=\"hidden\" name=\"import_status_id\" value=\"%s\"/>",$importStatusId)?>
	<ul id="toollist">
		<li><a href="?<?php printf("%s&step=1",$this->model->selected)?>">&lt; Back to Step 1.</a></li>
		<li><input type="submit" id="btnFindMatch" name="btnFindMatch" value="Cleanse!"/></li>
		<li><input type="submit" id="btnInsertNew" name="btnInsertNew" value="Create new players"/></li>
	</ul>
	<?php printf("<input type=\"hidden\" name=\"import_status_id\" value=\"%s\"/>",$importStatusId)?>	
</div>
<?php if(isset($_GET["msg"]) && trim($_GET["msg"]) != ""){?>
	<div class="success_banner"><b><?php echo $_GET["msg"]?></b></div>	
<?php }?>
<?php if(isset($_GET["errormsg"]) && trim($_GET["errormsg"]) != ""){?>
	<div class=""></div>
<?php }?>
<table cellspacing="0" cellpadding="0" border="1" width="100%">
	<tr>
		<th></th>
		<th>First<br/>Name</th>
		<th>Last<br/>Name</th>
		<th>Email<br/>Address</th>
		<th>&nbsp;</th>
	</tr>
<?php foreach($newPlayerList as $row){?>
	<?php printf("<tbody id=\"view_%s\" onclick=\"switchView(this)\">",$row->playerid)?>
		<tr>
			<?php 
			printf("<td><input type=\"checkbox\" name=\"rowID\" value=\"%s\"/></td>",$row->playerid);
			printf("<td><span id=\"span_first_%s\">%s</span></td>",$row->playerid,$row->firstName);
			printf("<td><span id=\"span_last_%s\">%s</span></td>",$row->playerid,$row->lastName);
			printf("<td><span id=\"span_emailAddr_%s\">%s</span></td>",$row->playerid,$row->emailAddress);
			?>
			<td></td>
		</tr>
	</tbody>
	<?php printf("<tbody id=\"edit_%s\" style=\"display:none;\">",$row->playerid)?>
		<tr>
			<?php 
			printf("<td><input type=\"checkbox\" name=\"rowID\" value=\"%s\"/></td>",$row->playerid);
			printf("<td><input type=\"text\" id=\"txt_first_%s\" name=\"first_%s\" value=\"%s\" onchange=\"textChange(this)\"/></td>",$row->playerid,$row->playerid,$row->firstName);
			printf("<td><input type=\"text\" id=\"txt_last_%s\" name=\"last_%s\" value=\"%s\" onchange=\"textChange(this)\"/></td>",$row->playerid,$row->playerid,$row->lastName);
			printf("<td><input type=\"text\" id=\"txt_emailAddr_%s\" name=\"emailAddr_%s\" value=\"%s\" onchange=\"textChange(this)\"/></td>",$row->playerid,$row->playerid,$row->emailAddress);
			?>
			<td>
				<input type="button" value="Save" onclick="saveRow(this)"/>
				<?php 
				printf("<input type=\"hidden\" id=\"orig_first_%s\" name=\"orig_first_%s\" value=\"%s\"/>",$row->playerid,$row->playerid,$row->firstName);
				printf("<input type=\"hidden\" id=\"orig_last_%s\" name=\"orig_last_%s\" value=\"%s\"/>",$row->playerid,$row->playerid,$row->lastName);
				printf("<input type=\"hidden\" id=\"orig_emailAddr_%s\" name=\"orig_emailAddr_%s\" value=\"%s\"/>",$row->playerid,$row->playerid,$row->emailAddress);
				?>
			</td>
		</tr>
	</tbody>
<?php }
if(count($newPlayerList) == 0){?>
	<tr>
		<td colspan="5" align="center">
			<strong>No new players found.</strong>
			<p>Proceed to <?php printf("<a href=\"?%s&step=4&import_status_id=%s\">",$this->model->selected,$importStatusId)?>next step</a>.</p>
		</td>
	</tr>	
<?php }?>
</table>